package com.hankol.newretail

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.manager.LoadingManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleUtils
import com.baselibrary.view.MarginDecoration
import com.baselibrary.activity.BaseActivity
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.newretail.adapter.StoreGoodsAdapter
import com.hankol.newretail.bean.GoodListBean
import com.hankol.newretail.databinding.ActivityAllGoodsBinding
import com.hankol.newretail.viewmodel.HomeMallViewModel


/**
 * @description 全部商品
 *@author wxp
 */
@Route(path = ActivityRouter.NEW_RETAIL.ALL_GOODS)
class AllGoodsActivity : BaseActivity() {

    var page = 1
    private var hasnext: Boolean = false
    private val adapter by lazy { StoreGoodsAdapter() }
    private lateinit var binding: ActivityAllGoodsBinding
    private lateinit var mViewModel: HomeMallViewModel
    var shopId = 0

    companion object {
        //key
        const val KEY_SHOP_ID = "shopId"
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_all_goods
    }

    override fun init() {
        binding = getDataBinding<ActivityAllGoodsBinding>()
        mViewModel = getActivityViewModelProvider(this).get(HomeMallViewModel::class.java)
        if (intent.extras.checkNotNull()) {
            if (ARouterUtils.parseParams(intent.extras!!, KEY_SHOP_ID).isNotEmpty()) {
                shopId = ARouterUtils.parseParams(intent.extras!!, KEY_SHOP_ID)[0].checkValue().toInt()
            }
        }
        LoadingManager.showLoadingDialog(this)
        initView()
        initListener()
        initObserver()
        loadData()
    }

    private fun initView() {
        binding.rvGoodList.layoutManager = GridLayoutManager(this, 2)
        binding.rvGoodList.addItemDecoration(MarginDecoration(this, 9))
        binding.rvGoodList.adapter = adapter
    }

    private fun initObserver() {
        mViewModel.allStoreGoodsLD.observe(this, Observer {
            LoadingManager.hideLoadingDialog(this)
            binding.smartRefresh.finishRefresh()
            if (it.data.size == 0) {
                //无数据
                if (page != 1) {
                    binding.smartRefresh.finishLoadMore()
                }
                adapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_GOOD_LIST, binding.rvGoodList, "暂无商品")
            } else {
                if (it.data.checkNotNull()) {
                    hasnext = it.hasnext
                    binding.smartRefresh.finishLoadMore()
                    if (page == 1) {
                        adapter.setNewData(it.data)
                    } else {
                        adapter.addData(it.data)
                    }
                    page++
                } else {
                    binding.smartRefresh.finishLoadMore()
                }
            }

        })

    }

    private fun initListener() {
        binding.ivBack.setOnClickListener { finish() }
        adapter.setOnItemClickListener { adapter, view, position ->
            if (!DoubleUtils.isFastDoubleClick()) {
                val bean = adapter.data[position] as GoodListBean
                //进入对应详情页面
                val map = java.util.HashMap<String, Any>()
                map[Constants.GOODS_DETAIL_ID] = bean.id
                map[Constants.GOODS_DETAIL_SOURCE] = "酒窖详情页"
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS, map)
            }
        }

        binding.smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
    }

    fun loadData() {
        mViewModel.reqAllStoreGoodsList(shopId, page)
    }
}